package com.example.driving.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.bumptech.glide.Glide;
import com.example.driving.R;
import com.example.driving.base.OnItemClickListener;
import com.example.driving.model.Subject2Bean;

import java.util.List;

public class Subject2Adapter extends RecyclerView.Adapter<Subject2Adapter.Subject2ViewHolder> {


    private final List<Subject2Bean> data;
    private OnItemClickListener listener;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    public Subject2Adapter(List<Subject2Bean> data) {
        this.data = data;
    }


    @NonNull
    @Override
    public Subject2ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video,
                parent, false);

        Subject2ViewHolder viewHolder = new Subject2ViewHolder(itemView);
        itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(v, viewHolder.getAdapterPosition());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull Subject2ViewHolder holder, int position) {
        Subject2Bean subject2Bean = data.get(position);
        Glide.with(holder.itemView).load("http://121.37.118.193/img/video/"+subject2Bean.getImgurl()).into((ImageView)holder.imageView);

//        holder.imageView.setImageURI(subject2Bean.getImgurl());
        holder.textView.setText(subject2Bean.getTitle());

    }

    @Override
    public int getItemCount() {
        System.out.println("getItemCount");
        System.out.println(data.size());
        return data.size();
    }

    public class Subject2ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public Subject2ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
        }
    }



}
